package com.oyb.service;

import com.oyb.dao.StuDao;
import com.oyb.entity.Page;
import com.oyb.entity.Student;
import com.oyb.util.ConnectionUtils;

import java.util.List;

public class StudentServies {
    private StuDao stuDao = new StuDao();
    private ClassServies classServies = new ClassServies();
//    //查询所有学生
//    public List<Student> queryAllStudent() {
//        return stuDao.queryAllStudent();
//    }
    //插入学生(新增)
    public int insertStudent(Student student){
        int result = 0;
        try {
            //开启事务
            ConnectionUtils.begin();
            //调用班级servies,传入班级id去修改班级人数
            result = stuDao.insertStudent(student);
            if(result>0){
                classServies.updateclasscount(student.getCid(), 1, 1);
            }
            ConnectionUtils.commit();

        }catch (Exception e){
            ConnectionUtils.roolback();
        }
        return result;
    }

    public Page queryStuPage(Page page){
        //查询所有学生的数量也就是记录的条数,放入page对象中
         int pageSum = stuDao.querystudentcount();
        page.setPageSum(pageSum);
        //计算当前的总页码 <- 总条数 每页多少条
        int pageCount = pageSum % page.getPageSize() == 0 ? pageSum / page.getPageSize() : pageSum / page.getPageSize()+1;
        page.setPageCount(pageCount);
        //调用dao层查询当前页的列表数据,并放入page对象中
        List<Student> students = stuDao.queryAllStudent(page);
        page.setDatas(students);
        return page;
    }
}
